Skip to content

Adding configurable compiler plugins for Bazel builds.#23212

Open
benvanik wants to merge 1 commit intomainfrom
users/benvanik/bazel-plugins
Open

Adding configurable compiler plugins for Bazel builds.#23212
benvanik wants to merge 1 commit intomainfrom
users/benvanik/bazel-plugins

Conversation

@benvanik
Copy link
Copy Markdown
Collaborator

@benvanik benvanik commented Jan 20, 2026

This implements configurable static compiler plugin selection for Bazel builds. Previously, Bazel hardcoded the contents of StaticLinkedPlugins.inc, so every Bazel-built compiler tool linked the same fixed plugin set with no supported way to exclude unavailable toolchains, speed up local iteration, or add downstream compiler plugins.

The core mechanism mirrors the shape of the existing Bazel HAL driver selection: //compiler/plugins:enabled_plugins is a string_list_flag, exposed through the --iree_compiler_plugins alias, and compiler/plugins/plugins.bzl generates the per-plugin config settings, .inc fragments, and aggregate dependency targets consumed by compiler/src/iree/compiler/PluginAPI/Config.

iree-bazel-configure now resolves IREE_COMPILER_PLUGINS from the local environment. It supports:

  • IREE_COMPILER_PLUGINS=all for every buildable plugin.
  • IREE_COMPILER_PLUGINS=all,-plugin_id for all buildable plugins except selected exclusions.
  • IREE_COMPILER_PLUGINS=plugin_a,plugin_b for explicit selection.

The wrapper defaults to all so local developer builds get a batteries-included compiler when the relevant SDKs and submodules are available. Explicit plugin selections fail loudly when a requested plugin is unknown or cannot be built.

Toolchain and SDK detection now covers:

  • CUDA, including IREE_CUDA_TOOLKIT_ROOT, IREE_CUDA_DEPS_DIR, common CUDA environment variables, and nvcc discovery, with the selected repository environment persisted for @iree_cuda.
  • ROCm/HIP, including ROCM_PATH, ROCM_ROOT, ROCM_HOME, HIP_PATH, and hipcc.
  • StableHLO and torch-mlir submodule availability.
  • Vulkan SDK discovery for LLVM's @vulkan_sdk//:sdk repository rule, using VULKAN_SDK, vulkaninfo, and common SDK install locations.

The Torch input plugin is now wired into Bazel. Bazel-built tools can link input_torch and process Torch dialect input through the existing Torch input conversion tests. The implementation uses torch-mlir's upstream Bazel torch_mlir_configure rule instead of carrying a copied overlay implementation. The Torch InputConversion CMake files are also generated from their BUILD files; the root Torch CMake file remains hand-maintained because it owns CMake-only torch-mlir vendor setup and plugin registration.

The plugin registry is also root-loadable for out-of-tree users. //compiler/plugins loads @//build_tools/bazel:default_compiler_plugins.bzl, so a downstream Bazel root can provide that file to register additional compiler plugins, replace registration targets, or choose different default plugin IDs without editing IREE. In the same spirit, IREE now creates bundled LLVM, StableHLO, and torch-mlir source repositories only when IREE is the root module; downstream roots can provide their own MLIR-adjacent repositories.

This PR intentionally does not add GPU architecture autodetection. Vulkan SDK discovery is just enough to make LLVM's Vulkan SDK repository rule work when a local SDK is available; choosing GPU-specific target defaults should be a separate policy discussion.

Validation performed locally:

  • iree-bazel-configure
  • iree-bazel-build //compiler/src/iree/compiler/PluginAPI/Config:StaticLinkedPlugins
  • iree-bazel-build //tools:iree-compile
  • iree-bazel-build //compiler/src/iree/compiler/PluginAPI/Config:StaticLinkedPlugins --iree_compiler_plugins=hal_target_llvm_cpu,input_tosa
  • select-aware bazel cquery checks confirming disabled Torch/StableHLO plugins are absent from the configured graph
  • iree-bazel-test //compiler/plugins/... --test_output=errors
  • CMake build/test coverage for the generated Torch InputConversion CMake files
  • pre-commit run --all
  • git diff --check

@benvanik benvanik added the infrastructure Relating to build systems, CI, or testing label Jan 20, 2026
@benvanik benvanik requested a review from maxbartel January 20, 2026 17:40
@benvanik benvanik marked this pull request as ready for review January 20, 2026 19:21
@benvanik benvanik requested a review from ScottTodd as a code owner January 20, 2026 19:21
Copy link
Copy Markdown
Contributor

@maxbartel maxbartel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The plugin infra looks solid. I have a suggestion on how to import torch-mlir though. Pretty cool!

Comment thread build_tools/bazel/workspace.bzl
Comment thread build_tools/bazel/extensions.bzl Outdated
Comment thread compiler/plugins/BUILD.bazel Outdated
Comment thread compiler/plugins/BUILD.bazel Outdated
Copy link
Copy Markdown
Contributor

@krzysz00 krzysz00 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a comment, can we get similar autodetection of whether there's a Vulkan SDK installed?

... and as a followup, if there'll be such autodetection, it might make sense to detect the gfx number of the running machine so that we can build e2e tests and the like? But that's probably a subsequent PR

Comment thread build_tools/bazel/workspace.bzl
Comment thread compiler/src/iree/compiler/PluginAPI/Config/BUILD.bazel
@benvanik benvanik force-pushed the users/benvanik/bazel-plugins branch from 13f7a0c to c562954 Compare April 28, 2026 04:13
@benvanik benvanik force-pushed the users/benvanik/bazel-plugins branch from c562954 to 1e8f4cc Compare April 28, 2026 04:28
@benvanik
Copy link
Copy Markdown
Collaborator Author

feedback addressed and vulkan sdk check added

Replace the hardcoded Bazel StaticLinkedPlugins.inc contents with a generated registry driven by //compiler/plugins:enabled_plugins and the --iree_compiler_plugins alias. This mirrors the runtime driver selection model while preserving the existing default plugin set for plain Bazel invocation.

Teach iree-bazel-configure/configure_bazel.py to resolve IREE_COMPILER_PLUGINS, including all/exclusion syntax, from buildable local inputs. The wrapper defaults to all buildable plugins so developer builds pick up optional inputs such as Torch when the submodule is initialized, while explicit plugin lists fail on unknown or unavailable entries. CUDA selection also records the repository env var consumed by @iree_cuda so plugin selection and external repository configuration agree.

Make the compiler plugin registry root-loadable so a downstream Bazel root can provide its own default_compiler_plugins.bzl and register out-of-tree plugins without editing IREE. Align MLIR-adjacent source repositories with the existing LLVM ownership model: IREE creates bundled LLVM, StableHLO, and torch-mlir sources when it is the root module, while downstream roots can provide their own repositories and configure torch-mlir through its upstream Bazel overlay.

Add Bazel wiring for the Torch input plugin. This lets Bazel-built tools link input_torch and process Torch dialect input through the same conversion paths covered by the existing lit tests. Those tests are only compatible with tools built with input_torch enabled, and the generated CMake preserves the same option guard.

Teach bazel_to_cmake to preserve supported compiler-plugin compatibility selects, alongside the existing platform selects, so generated CMake does not silently drop option-gated lit tests.

Declare the LLVMIR translation helper's dialect translation dependencies on the helper target itself, matching the headers it includes instead of relying on consumers to expose incidental MLIR translation libraries.

Detect Vulkan SDK installs during Bazel configuration and persist the normalized VULKAN_SDK repository environment for LLVM's Vulkan SDK repository rule when available.
@benvanik benvanik force-pushed the users/benvanik/bazel-plugins branch from 1e8f4cc to 25c54e5 Compare April 28, 2026 06:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

infrastructure Relating to build systems, CI, or testing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants